package com.example.touristassist;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

public class ChatContentProvider extends ContentProvider {
	static String AUTHORITY = "com.example.stockportfolio";
	static String BASE_PATH = "chat";
	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);
	static final int ALL_ITEMS = 1;
	static final int SINGLE_ITEM = 2;
	
	private SQLiteDatabase db;
	private DBTable table;
	
	static final UriMatcher uriMatcher;
	static {
	   uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
	   uriMatcher.addURI(AUTHORITY, BASE_PATH, ALL_ITEMS);
	   uriMatcher.addURI(AUTHORITY, BASE_PATH + "/#", SINGLE_ITEM);
	}
	
	Context context;
	
	public ChatContentProvider() { }
	
	@Override
	public boolean onCreate() {
		this.context = getContext();
		this.table = new DBTable(context, BASE_PATH, null, DBMetadata.Version);
		try {
			this.db = this.table.getWritableDatabase();
		} catch(Exception ex) {
			this.db = this.table.getReadableDatabase();
		}
		return ((this.table == null) ? false : true);
	}
	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		switch (uriMatcher.match(uri)) {
	    case SINGLE_ITEM :
	    	String rowID = uri.getPathSegments().get(1);
	    	if (selection.length() > 0)
	    		selection += " and " + table.getColumns()[0] + "=" + rowID;
		}

		int result = db.delete(table.getName(), selection, null);
    	this.context.getContentResolver().notifyChange(uri, null);
    	return result;
	}

	@Override
	public String getType(Uri uri) {
		switch(uriMatcher.match(uri)) {
			case ALL_ITEMS :
				return "vnd.android.cursor.dir/vnd.pk.elemental";
			case SINGLE_ITEM :
				return "vnd.android.cursor.item/vnd.pk.elemental";
			default: 
				throw new IllegalArgumentException("Unsupported URI : " + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		long id = db.insert(table.getName(), null, values);
		
		Uri insertedId = ContentUris.withAppendedId(uri, id);
		this.context.getContentResolver().notifyChange(insertedId, null);
		
		return insertedId;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, 
			String[] selectionArgs, String sortOrder) {
		
		SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        builder.setTables(table.getName());
        
		switch (uriMatcher.match(uri)) {
	        case SINGLE_ITEM : 
	        	String rowID = uri.getPathSegments().get(1);
	        	builder.appendWhere(table.getColumns()[0] + "=" + rowID);
	        default: break;
		}
		
		Cursor c = builder.query(db, projection, selection, null, null, sortOrder, null);
        c.setNotificationUri(this.context.getContentResolver(), CONTENT_URI);
        return c;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, 
			String[] selectionArgs) {
		
		switch (uriMatcher.match(uri)) {
	    case SINGLE_ITEM :
	    	String rowID = uri.getPathSegments().get(1);
	    	if (selection.length() > 0)
	    		selection += " and " + table.getColumns()[0] + "=" + rowID;
		}
		
		int result = db.update(table.getName(),values, selection, null);
		this.context.getContentResolver().notifyChange(uri, null);
		return result;
	}
}